/**
 * 
 */
package components;

import java.security.InvalidParameterException;
import java.security.spec.InvalidParameterSpecException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.example.remotecontrol.BuildConfig;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import common.Constant;

import data.RCDevice;
import data.RCAlertSMSCommand;
import data.RCDeviceSMSCommand;

/**
 * @author trantrungkien
 * 
 */
public class AlarmManager {
	private static AlarmManager mAlarmManager = null;
	private ArrayList<RCDevice> mDeviceList;
	private final static String SMS_ALARM_KEY = "SMS_ALARM_KEY";
	private final static int DEVICE_COUNT = 8;
	private Context mContext = null;
	private boolean mIsUpdating;

	private AlarmManager() {

	}

	public static AlarmManager sharedAlarmList(Context context) {
		if (mAlarmManager == null) {
			mAlarmManager = new AlarmManager();
			mAlarmManager.loadAlarmList(context);

		}

		return mAlarmManager;
	}

	public List<RCDevice> getDevices() {
		return Collections.unmodifiableList(mDeviceList);
	}

	private void loadAlarmList(Context context) {
		// TODO Auto-generated method stub
		mContext = context;
		SharedPreferences pref = context.getSharedPreferences(
				Constant.KEY_SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		String s = pref.getString(SMS_ALARM_KEY, null);

		ArrayList<RCDevice> result = new ArrayList<RCDevice>();

		if (s == null) {
			result = initAlarmList();
		} else {
			try {
				RCDevice[] rcDevices = new Gson().fromJson(s, RCDevice[].class);

				for (int i = 0; i < rcDevices.length; i++) {
					rcDevices[i].setDeviceIndex(i);
				}

				result.addAll(Arrays.asList(rcDevices));

			} catch (JsonSyntaxException ex) {
				result = initAlarmList();
			}

		}

		mDeviceList = result;

	}

	private ArrayList<RCDevice> initAlarmList() {
		ArrayList<RCDevice> result = new ArrayList<RCDevice>();
		for (int i = 0; i < DEVICE_COUNT; i++) {
			RCDevice alarm = new RCDevice(i);
			alarm.setName(getAlarmName(i));
			alarm.setAlert(false);
			
			
			result.add(alarm);
		}
		
//		if (BuildConfig.DEBUG) {
//			result.get(3).setAlert(true);
//			result.get(3).setIsOn(true);
//		}

		return result;
	}

	private String getAlarmName(int deviceIndex) {
		return "Thiết bị " + (deviceIndex + 1);
	}

	/**
	 * 
	 * @param context
	 * @return
	 * @exception InvalidParameterException
	 */
	private void saveAlarmList() {

		if (mContext == null) {
			throw new InvalidParameterException();
		}
		String json = new Gson().toJson(mDeviceList);

		SharedPreferences pref = mContext.getSharedPreferences(
				Constant.KEY_SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = pref.edit();

		editor.putString(SMS_ALARM_KEY, json);
		editor.commit();
	}

	public void changeAlertState(RCAlertSMSCommand alertCommand) {

		if (alertCommand == null) {
			throw new InvalidParameterException();
		}

		// mAlarmList = new ArrayList<Alarm>();
		int alarmCount = mDeviceList.size();

		// Refresh state
		for (RCDevice alarm : mDeviceList) {
			alarm.setAlert(false);
			alarm.setDate(new Date());
		}

		for (Integer deviceIndex : alertCommand.getActiveDeviceIndexes()) {

			if (deviceIndex <= alarmCount) {
				RCDevice device = mDeviceList.get(deviceIndex);
				device.setAlert(true);
			}
		}

		saveAlarmList();
	}

	public void changeDeviceState(RCDeviceSMSCommand deviceStateCommand) {

		if (deviceStateCommand == null) {
			throw new InvalidParameterException();
		}

		// mAlarmList = new ArrayList<Alarm>();
		int alarmCount = mDeviceList.size();

		// Refresh state
		for (RCDevice alarm : mDeviceList) {
			alarm.setIsOn(false);
			alarm.setDate(new Date());
		}

		for (Integer deviceIndex : deviceStateCommand.getActiveDeviceIndexes()) {

			if (deviceIndex <= alarmCount) {
				RCDevice device = mDeviceList.get(deviceIndex);
				device.setIsOn(true);
			}
		}

		saveAlarmList();
	}

	public boolean isAlertAlarm() {
		for (RCDevice alarm : mDeviceList) {
			if (alarm.isAlert()) {
				return true;
			}
		}

		return false;
	}

	public boolean isOnDevice() {
		for (RCDevice alarm : mDeviceList) {
			if (alarm.isOn()) {
				return true;
			}
		}

		return false;
	}

	/**
	 * @return the mIsUpdating
	 */
	public boolean isUpdating() {
		return mIsUpdating;
	}

	/**
	 * @param mIsUpdating
	 *            the mIsUpdating to set
	 */
	public void setIsUpdating(boolean mIsUpdating) {
		this.mIsUpdating = mIsUpdating;
	}

	/**
	 * 
	 * @param item
	 * @param newName
	 * @throws InvalidParameterSpecException
	 */
	public void changeDeviceName(RCDevice item, String newName)
			throws InvalidParameterSpecException {

		for (RCDevice device : mDeviceList) {
			if (device == item) {
				continue;
			}

			if (device.getName().equalsIgnoreCase(newName)) {
				throw new InvalidParameterSpecException(
						"Tên thiết bị đã tồn tại");
			}
		}

		item.setName(newName);

		saveAlarmList();

	}
}
